package com.excel.analysis;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class GsonUtils {

	public static Gson getGson() {
		Gson gson = new Gson();
		return gson;
	}

	public static JsonObject getValues(String value) {
		if (value != null) {
			value = value.replaceAll("#", "");
			JsonParser jp = new JsonParser();
			JsonObject jsonObject = (JsonObject) jp.parse(value);
			return jsonObject;
		} else {
			return null;
		}
	}

	public static String getKey(String value, String key) {
		JsonObject jo = getValues(value);
		Object o = jo.get(key);
		if (o != null) {
			return jo.get(key).getAsString();
		} else {
			return null;
		}
	}

	public static String getKeyByValue(JsonObject jo, String key) {
		Object o = jo.get(key);
		if (o != null) {
			return jo.get(key).getAsString();
		} else {
			return null;
		}
	}

	public static String createCode(int i) throws Exception {
		if(i >= 999) {
			throw new Exception("num is too larger");
		}
		String code = null;
		int len = String.valueOf(i).length();
		for (int j = len; j < 3; j++) {
			if(code != null) {
				code += "0";
			} else {
				code = "0";
			}
		}
		code += i++;
		return code;
	}
}
